/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mascotikas.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mascotikas.client.jdos.ImageInfo;
import com.mascotikas.client.services.IUserImageService;
import com.mascotikas.client.services.IUserImageServiceAsync;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.*;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.*;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Unisysad
 */
public class FormDataImage extends DynamicForm {

    Canvas parent;
    private RichTextEditor richTextEditor;
    private String photoKey;
    private Button validateItem;
    IUserImageServiceAsync userImageService = GWT.create(IUserImageService.class);

    public FormDataImage(final Canvas parent) {
        final Map<String, String> mapCategory = new HashMap<String, String>();
        mapCategory.put("Perritos", "dogs");
        mapCategory.put("Avesitas", "birds");
        mapCategory.put("Gatikos", "cats");
        mapCategory.put("Otrikos", "others");

        final ImageInfo infoImage = new ImageInfo();
        this.parent = parent;
        final DataSource dataSource = new DataSource();
        richTextEditor = new RichTextEditor();
        richTextEditor.setHeight(100);
        richTextEditor.setWidth(700);
        richTextEditor.setOverflow(Overflow.HIDDEN);
        richTextEditor.setCanDragResize(false);
        richTextEditor.setShowEdges(false);
        richTextEditor.setID("comment");  
        setDataSource(dataSource);
        setUseAllDataSourceFields(true);

        HeaderItem header = new HeaderItem();
        header.setDefaultValue("Ingresa informacion acerca de tu Mascota");

        final TextItem textNameItem = new TextItem("name", "Nombre Mascoti-k");
        textNameItem.setRequired(Boolean.FALSE);
        textNameItem.setLength(50);

        final TextItem textUbicacionItem = new TextItem("locate", "Ubicacion");
        textUbicacionItem.setRequired(Boolean.FALSE);
        textUbicacionItem.setLength(50);

        CheckboxItem acceptItem = new CheckboxItem();
        acceptItem.setName("acceptTerms");
        acceptItem.setTitle("I accept the terms of use.");
        acceptItem.setRequired(true);
        acceptItem.setWidth(150);

        final ComboBoxItem categoryItem = new ComboBoxItem("category");
        categoryItem.setTitle("Selecciona Categoria");
        categoryItem.setValueMap("Perritos", "Gatikos", "Avesitas", "Otrikos");
        categoryItem.setDefaultValue("Perritos");
        categoryItem.setRequired(Boolean.TRUE);

        final TextItem textPriceItem = new TextItem("price");
        textPriceItem.setTitle("Ingresa Un precio para tu mascota");
        textPriceItem.setRequired(Boolean.FALSE);
        textPriceItem.setDisabled(Boolean.TRUE);
        textPriceItem.setHint("Solo Numerico<br>");
        textPriceItem.setKeyPressFilter("[0-9.]");

        final ComboBoxItem typePublishItem = new ComboBoxItem("typePublish");
        typePublishItem.setTitle("Tipo Publicacion");
        typePublishItem.setValueMap("Venta", "Adopcion");
        typePublishItem.setDefaultValue("Adopcion");
        typePublishItem.setRequired(Boolean.TRUE);
        typePublishItem.addChangeHandler(new ChangeHandler()  {

            public void onChange(ChangeEvent event) {
                if (!typePublishItem.getValueAsString().equals("Venta")) {
                    textPriceItem.setDisabled(Boolean.FALSE);
                    textPriceItem.setRequired(Boolean.TRUE);
                } else {
                    textPriceItem.setDisabled(Boolean.TRUE);
                    textPriceItem.setRequired(Boolean.FALSE);
                }
            }
        });

        validateItem = new Button();
        validateItem.setTitle("Actualizar");
        validateItem.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {

            public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
                if (validate(false)) {
                    infoImage.setComment(richTextEditor.getValue());
                    infoImage.setPhotoKey(photoKey);
                    infoImage.setCategory(mapCategory.get(getValueAsString("category")));
                    infoImage.setLocate(getValueAsString("locate"));
                    infoImage.setName(getValueAsString("name"));
                    infoImage.setTypePublish(getValueAsString("typePublish"));
                    String price = getValueAsString("price");
                    if (price != null && !price.equals("")) {
                        infoImage.setPrice(price);
                    }
                    userImageService.setDataImage(infoImage, new AsyncCallback<String>() {

                        @Override
                        public void onFailure(Throwable caught) {
                            // TODO Auto-generated method stub
                        }

                        public void onSuccess(String result) {
                            final Window winModal = new Window();
                            winModal.setAutoSize(Boolean.TRUE);

                            winModal.setTitle("OK");
                            winModal.setShowMinimizeButton(false);
                            winModal.setCanDragReposition(Boolean.FALSE);
                            winModal.setIsModal(true);
                            winModal.setShowModalMask(true);
                            winModal.setAutoCenter(Boolean.TRUE);;
                            winModal.addItem(new Label(result));
                            winModal.show();
                        }
                    });
                    destroy();
                    parent.destroy();
                }
            }
        });

        setFields(header, textNameItem, categoryItem, textUbicacionItem, typePublishItem, textPriceItem, acceptItem);
        draw();
    }

    public RichTextEditor getRichTextEditor() {
        return richTextEditor;
    }

    public void setRichTextEditor(RichTextEditor richTextEditor) {
        this.richTextEditor = richTextEditor;
    }

    public String getPhotoKey() {
        return photoKey;
    }

    public void setPhotoKey(String photoKey) {
        this.photoKey = photoKey;
    }

    public Button getValidateItem() {
        return validateItem;
    }

    public void setValidateItem(Button validateItem) {
        this.validateItem = validateItem;
    }
    
}
